﻿using System;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WhileAwayWpf {
    /// <summary>
    /// App
    /// </summary>
    public partial class App : System.Windows.Application {

        static System.Threading.Mutex mtx = null;
        /// <summary>
        /// InitializeComponent
        /// </summary>
        public void InitializeComponent() {

            if ( Environment.GetCommandLineArgs().Length == 2 && Environment.GetCommandLineArgs()[1].ToLower().StartsWith("/c"))
            {
                bool created;
                mtx = new System.Threading.Mutex(true, "C4F-WhileAway", out created);
                
                // If we created the mutex then admin mode isn't already runing.  Otherwise
                // no StartupUri and we will die.
                if( created )
                    this.StartupUri = new System.Uri("AdminWindow.xaml", System.UriKind.Relative);
            }
            else if (Environment.GetCommandLineArgs().Length == 2 || Environment.GetCommandLineArgs()[1].ToLower().StartsWith("/s"))
                this.StartupUri = new System.Uri("ScreenSaverWindow.xaml", System.UriKind.Relative);

            // No support for preview -- /p:####### (window handle)

			Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
        }
        
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        public static void Main() {
            WhileAwayWpf.App app = new WhileAwayWpf.App();
            app.InitializeComponent();

            if (app.StartupUri != null)
                app.Run();
            else app.Shutdown(0);

            if (mtx != null) mtx.Close();
        }

        private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
        		MessageBox.Show("Fatal error: " + e.Exception.Message + "\n" + e.Exception.StackTrace);
        }
    }
}
